﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Kinect;

namespace kinect8.Toolkit
{
    public class ZoomGestureDetector : GestureDetector
    {
        bool trackStarted;

        double ZDiffThresh = 0.1;
        double YDiffThresh = 0.1;
        double YBetweenHandAndElbow = 0.1;
        double ZBetweenHandAndElbow = 0.1;
        double leftX;
        double rightX;

        public ZoomGestureDetector()
        {
            trackStarted = false;
            Enable = true;
        }
        public override bool TrackGesture(Skeleton skeleton)
        {
            if (skeleton == null)
            {
                return false;
            }
            bool isInPosture = IsInPosture(skeleton);
            if (trackStarted)
            {
                if (isInPosture)
                {
                    double orignDistance = rightX - leftX;
                    double currentDistance = skeleton.Joints[JointType.HandRight].Position.X -
                        skeleton.Joints[JointType.HandLeft].Position.X;
                    RaiseGestureDetected(new GestureEventArgs
                            ( "Zoom", 1.0, "finish", currentDistance / orignDistance));
                    rightX = skeleton.Joints[JointType.HandRight].Position.X;
                    leftX = skeleton.Joints[JointType.HandLeft].Position.X;

                    return true;
                }
                else
                {
                    trackStarted = false;
                    return false;
                }

            }
            else
            {
                if (isInPosture)
                {
                    rightX = skeleton.Joints[JointType.HandRight].Position.X;
                    leftX = skeleton.Joints[JointType.HandLeft].Position.X;
                    trackStarted = true;
                    return false;
                }
                else
                {
                    return false;
                }
            }

            //return false;
        }
        private bool IsInPosture(Skeleton skeleton)
        {
            SkeletonPoint leftHand = skeleton.Joints[JointType.HandLeft].Position;
            SkeletonPoint rightHand = skeleton.Joints[JointType.HandRight].Position;
            SkeletonPoint leftElbow = skeleton.Joints[JointType.ElbowLeft].Position;
            SkeletonPoint rightElbow = skeleton.Joints[JointType.ElbowRight].Position;

            if (Math.Abs(leftHand.Z - rightHand.Z) > ZDiffThresh)
            {
                //Console.WriteLine("zdiff:" + Math.Abs(leftHand.Z - rightHand.Z));

                return false;
            }
            if (Math.Abs(leftHand.Y - rightHand.Y) > YDiffThresh)
            {
                //Console.WriteLine("ydiff:" + Math.Abs(leftHand.Y - rightHand.Y));

                return false;
            }
            if (leftHand.Y - leftElbow.Y < YBetweenHandAndElbow)
            {
                //Console.WriteLine("YBetweenHandAndElbowleft:" + (leftHand.Y - leftElbow.Y));

                return false;
            }
            if (rightHand.Y - rightElbow.Y < YBetweenHandAndElbow)
            {
                //Console.WriteLine("YBetweenHandAndElbowright:" + (rightHand.Y - rightElbow.Y));

                return false;
            }
            if (leftElbow.Z - leftHand.Z < ZBetweenHandAndElbow)
            {
                //Console.WriteLine("ZBetweenHandAndElbowleft:" + (leftElbow.Z - leftHand.Z));

                return false;
            }
            if (rightElbow.Z - rightHand.Z < ZBetweenHandAndElbow)
            {
                //Console.WriteLine("ZBetweenHandAndElbowright:" + (rightElbow.Z - rightHand.Z));

                return false;
            }
            return true;
        }

    }
}
